import java.util.ArrayList;

/**
 * This is the stubbed out version of the main control for the game called 24/7.
 * 
 * @author Alex Gaskill and Ariel Wilson
 * @version 1.5
 * @see Board, Player, tileSet
 */
public class MasterControl 
{
	
	/**
	 * this creates the players and stores them in to the player array.
	 * @param numberPlayers - the number of players that want to play the game
	 * (it is assumed that the number of players from 2 to 4.)
	 */
	public MasterControl(int numberPlayers)
	{
		_playerCount = numberPlayers;
		_playerArray = new ArrayList<Player>(4);
		_myTileSet = new TileSet();
                _myBoard = new Board(_myTileSet.getTile());
                _player = _playerCount % (_playerArray.size() - 1);
		
		for (int i = 0; i < numberPlayers; i++)
		{
                    Player temp = new Player();

                    while(temp.checkHand())
                    {
                        temp.setHand(_myTileSet.getTile());
                    }
                    _playerArray.add(temp);
		}
	}
	
	/**
	 * Allows the player to make a move on the board
	 * @param x : the x axis of where the player wants to place the tile
	 * @param y : the y axis of where the player wants to place the tile
	 * @param tile : the tile that the player wants to place on the board
	 * @return the score for that player to return to the GUI
	 */
	public int makeMove (int x, int y, int tile)
        {
            int score = -1;
		/*
		 * call myBoard.makemove with the same params as this method and expect
		 * to get an integer back call it score. Then check score and see if it is
		 * greater then or equal to 0 if so then call the playerArray (PA) with 
		 * the specific player in mind (playerArray.get(player)) and call that classes
		 * setScore method with the integer you checked as its parameter. And 
		 * then call the playerArray.get(player) call that classes getScore method
		 * and save that integer in to the integer that you checked. 
		 * finally return that integer.
		 */
       if (!_playerArray.get(_player).getHasMoved())
       {
          score = _myBoard.placeTile(x, y, tile);
		
          if (score >= 0)
          {
             _playerArray.get(_player).setScore(score);
             score = _playerArray.get(_player).getScore();
             _playerArray.get(_player).setHasMoved(true);
             _playerArray.get(_player).removeTile(tile);
          }
       }
        return score;
   }
	
	/**
	 * this ends the turn and moves the turn to the next player
	 */
	public void endTurn()
	{
		/*
		 * increment the playerCount and take the player and set that equal to
		 * playerCount mod by the PA length-1.
		 */
                _playerArray.get(_player).setHasMoved(false);
		_playerCount++;
		_player = _playerCount % _playerArray.size();
	}
	
	/**
	 * gets the current players current Score
	 * @return the players current score
	 */
	public int getPlayerScore()
	{
		return _playerArray.get(_player).getScore();
	}
	
	/**
	 * gets the hand of the current Player
	 * @return the players hand in a arrayList
	 */
	public ArrayList<Integer> getHand()
	{
		return _playerArray.get(_player).getHand();
	}
	
	/**
	 * allows the player to draw a tile into their hand and then ends the turn
	 * when appropriate
	 */
	public void drawTile()
	{
            int tile;
		/*
		 * if the players hand is not full (PA.get(_player).checkHand()) then
		 * call the TileSet method getTile() and save that as tile and then 
		 * call the players DrawTile method with the parameter of tile 
		 * (PA.get(_player).drawTile(tile)). Finally call the endTurn method.
		 */
            tile = _myTileSet.getTile();

            _playerArray.get(_player).drawTile(tile);
	}
	
  /**
   * gets the remaining tiles from the tileSet
   */
	public int getRemainingTiles()
	{
		return _myTileSet.pileSize();
	}
   
  /**
   * grabs the current state of the board
   */
  public int[][] getBoard()
  {
      return _myBoard.getBoard();
  }
  
  /**
   * gets the current player
   */
  public int getPlayer()
  {
    return _player;
  }	
  
  /**
   * gets if the player has moved or not.
   */
  public boolean getHasMoved()
    {
        return _playerArray.get(_player).getHasMoved();
    }
    
	// class attributes
	private int _playerCount;
	private int _player;
	private ArrayList<Player> _playerArray;
	private Board _myBoard;
	private Player _players;
	private TileSet _myTileSet;
  private boolean _hasMoved;

}

